package org.example.elasticsearch.deserialize;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.example.utils.DateUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: sql
 * @date: 2024/6/7
 * @Description: 重写反序列化
 * @version: 1.0
 */
public class ESJsonDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = jp.getText();
        try {
            //return format.parse(date);
            return DateUtil.parseDate(date);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
